package com.example.test.chapter10.menuxml;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.example.test.R;
import com.example.test.chapter10.MainChapter10;

public class InflatingMenuActivity extends Activity {
	private final static Map<Integer, String> MESSAGES;
	private boolean groupItemsVisible = false;
	private Menu theMenu = null;
	static {
		MESSAGES = new HashMap<Integer, String>();
		MESSAGES.put(R.id.stop_menu_xml, "Stop it!");
		MESSAGES.put(R.id.dumb_menu_xml, "Dumb and dumber");
		MESSAGES.put(R.id.group_item1_menu_xml, "Group item1");
		MESSAGES.put(R.id.group_item2_menu_xml, "Group item2");
		MESSAGES.put(R.id.do_something_menu_xml, "Do something");
		MESSAGES.put(R.id.do_nothing_menu_xml, "Do nothing");
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainmenuxml);
		Button btn = (Button) findViewById(R.id.backpage);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),
						MainChapter10.class);
				startActivity(intent);
				finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		theMenu = menu;
		new MenuInflater(getApplicationContext()).inflate(R.menu.my_menu_xml,
				menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.do_nothing_menu_xml) {
			groupItemsVisible = !groupItemsVisible;
			theMenu.setGroupVisible(R.id.group_items_menu_xml,
					groupItemsVisible);
		}
		String message = MESSAGES.get(item.getItemId());
		if (message != null) {
			Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
			return (true);
		}
		return super.onOptionsItemSelected(item);
	}
}
